import 'package:flutter/cupertino.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/ChooserBox.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';

import '../../Util/HttpGo.dart';

class DialogChooseInterest extends StatefulWidget {
  Function() callback;

  DialogChooseInterest(this.callback);

  @override
  State<StatefulWidget> createState() {
    return _DialogChooseInterest();
  }
}

class _DialogChooseInterest extends State<DialogChooseInterest> {
  List<dynamic> interests = [{}, {}, {}, {}, {}];
  List<dynamic> allInterests = [];

  getInterest() {
    HttpGo(context).get("/Profile/GetInterestByStudent?studentId=${Global.studentId}", {}, (data) {
      allInterests = data["interests"];
      setState(() {});
    }, (code, error) => showToast(error));
  }

  chooseInterest(sortIndex) {
    showWheelDialog2(context, "", allInterests, "interestType", (index) {
      var interest = allInterests[index];
      for (int i = 0; i < 5; i++) {
        if (interests[i]["interestTypeId"] == interest["interestTypeId"]) {
          interests[i] = {};
        }
      }
      interests[sortIndex] = interest;
      setState(() {});
    });
  }

  setInterest() {
    List<int> interestIds = [];
    for (int i = 0; i < 5; i++) {
      if (interests[i]["interestTypeId"] != null) {
        interestIds.add(interests[i]["interestTypeId"]);
      }
    }
    HttpGo(context).post("/GroupLesson/SetupGroupLessonInterest", {"InterestSeq": interestIds}, (data) {
      widget.callback.call();
      PageUtil().back(context);
    }, (code, error) => showToast(error));
  }

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getInterest();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.fromLTRB(16, 16, 16, 16),
        margin: EdgeInsets.all(18),
        decoration: Global.getWhiteBg(8),
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Text("選擇喜愛的主題", style: TextStyle(fontSize: 15, color: MyColor.Green, fontWeight: FontWeight.bold)),
          Container(height: 0.8, color: MyColor.Green, margin: EdgeInsets.only(top: 8, bottom: 16)),
          Text("您好，因為您是第一次上該團體課程，請協助我們挑選您想要上課的主題，以便於之後進行課程的配對。"),
          Container(height: 8),
          Row(children: [
            LocalImage("chat.png", width: 24),
            Text("您之後也可在個人資料維護中進行維護", style: TextStyle(color: Color(0xff5f9ea0))),
          ]),
          Container(height: 16),
          Row(children: [
            Text("1.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
            Expanded(
                child: ChooserBox(interests[0]["interestType"] ?? "", onTap: () {
              chooseInterest(0);
            }))
          ]),
          Container(height: 8),
          Row(children: [
            Text("2.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
            Expanded(
                child: ChooserBox(interests[1]["interestType"] ?? "", onTap: () {
              chooseInterest(1);
            }))
          ]),
          Container(height: 8),
          Row(children: [
            Text("3.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
            Expanded(
                child: ChooserBox(interests[2]["interestType"] ?? "", onTap: () {
              chooseInterest(2);
            }))
          ]),
          Container(height: 8),
          Row(children: [
            Text("4.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
            Expanded(
                child: ChooserBox(interests[3]["interestType"] ?? "", onTap: () {
              chooseInterest(3);
            }))
          ]),
          Container(height: 8),
          Row(children: [
            Text("5.　", style: TextStyle(fontSize: 14, color: MyColor.Orange, fontWeight: FontWeight.bold)),
            Expanded(
                child: ChooserBox(interests[4]["interestType"] ?? "", onTap: () {
              chooseInterest(4);
            }))
          ]),
          Container(height: 24),
          Button("确定", height: 36, bgColor: MyColor.Green, radius: 4, onTap: setInterest)
        ]));
  }
}
